/***********************************************************************************
 * 文件名： mcu_ble.c
 * 作者： 刘言
 * 版本： 1
 * 说明：
 * 		BLE 抽象层，统一BLE访问接口。
 * 修改记录：
 * 	2022/10/28: 初版。 刘言。
***********************************************************************************/

#include "mcu_ble.h"
#include "peripheral.h"

static u8 mOwnMac[6];
static u8 mConnMac[6];
static u8 mConnMacType;
static ble_gap_st_t mState = BLE_GAP_ST_INIT;
static ble_gap_event_cb_t mCb = NULL;

// GAP状态改变事件
void _Ble_GapStateChanged(gaprole_States_t newState)
{
    switch ( newState )
    {
        case GAPROLE_STARTED:
            GAPRole_GetParameter(GAPROLE_BD_ADDR, mOwnMac);
            mState = BLE_GAP_ST_STARTED;
            break;
        case GAPROLE_ADVERTISING:
            mState = BLE_GAP_ST_ADVERTISING;
            break;
        case GAPROLE_CONNECTED:
			GAPRole_GetParameter(GAPROLE_CONN_BD_ADDR_TYPE,&mConnMacType);
		    GAPRole_GetParameter(GAPROLE_CONN_BD_ADDR,mConnMac);
            mState = BLE_GAP_ST_CONNECTED;
            break;
        case GAPROLE_CONNECTED_ADV:
            mState = BLE_GAP_ST_CONNECTED_ADV;
            break;
        case GAPROLE_WAITING:
			mState = BLE_GAP_ST_WAITING;
            break;
        case GAPROLE_WAITING_AFTER_TIMEOUT:
			mState = BLE_GAP_ST_WAITING_AFTER_TIMEOUT;
            break;
        case GAPROLE_ERROR:
            mState = BLE_GAP_ST_ERROR;
			LOG("Error\n");
            break;
        default:
            break;
    }
    if(mCb)mCb(mState);
}



IN_LINE void Ble_SetGapEventCb(ble_gap_event_cb_t new_cb)
{
    mCb = new_cb;
}

IN_LINE ble_gap_st_t Ble_GetGapState()
{
    return mState;
}

IN_LINE u8 * Ble_GetOwnMac()
{
    return mOwnMac;
}

IN_LINE u8 * Ble_GetConnMac()
{
    return mConnMac;
}


